/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.digwener.ui;

import com.digwener.entities.connectors.ConnectorHolder;
import com.digwener.db.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*; 
import java.io.*;
/**
 *
 * @author Anton
 */




public class MainForm extends JFrame {
    
    JPanel controlPanel = null;
    JLabel dbInstanceLabel = null;    
    
    JPanel bottomPanel = null;
    
    JPanel bottomOne = null;
    JPanel bottomTwo = null;

    JPanel controlOne = null;
    JPanel controlTwo = null;
    
    
    JButton connectButton = null;
    JTextField userTextField = null;    
    RespList respList = null;
    
    JTextField fileName = null;
    
    JButton saveAsButton = null;
    JButton saveButton = null;
    
    JButton loadButton = null;
        
    JButton fromDBButton = null;
    
    JButton toDBButton = null;
   
    DBConnection dbConnection = DBConnection.getInstance();
    
    InstanceList instanceList = null;
    
    JFileChooser fileChooser = null;
    
    JButton selectAllButton = null;
    JButton deselectAllButton = null;
    JButton deleteSelectedButton = null;
    
    
    final public String getUserFileName(){
        String res = "default.xml";
        
        try{
            BufferedReader bf = new BufferedReader(new FileReader("settings"));
            res = bf.readLine();
            bf.close();
        }catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
        return res;
    }
    
    public void setUserFileName(String fileName){
        try{
            BufferedWriter bw = new BufferedWriter(new FileWriter("settings"));
            bw.write(fileName);
            bw.close();
        } catch (IOException ex){
            System.out.println(ex.getMessage());
        }
               
    }
    
    public MainForm(String title){
        super(title);
        String s = "string";
        System.out.println(s.substring(0,s.length()-1));
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setSize(700,400);
        this.setLocationRelativeTo(null);
        this.setLayout(new BorderLayout());
        
        controlPanel = new JPanel(new BorderLayout());
        this.add(controlPanel,BorderLayout.NORTH);
        //this.add(controlPanel);
        
        controlOne = new JPanel(new FlowLayout());
        controlTwo = new JPanel(new FlowLayout());
        
        controlPanel.add(controlOne,BorderLayout.NORTH);
        controlPanel.add(controlTwo,BorderLayout.SOUTH);

        
        dbInstanceLabel = new JLabel("DB Instance");
        
        connectButton = new JButton("Connect");
        
        instanceList = new InstanceList();
        instanceList.setConnectorHolder(ConnectorHolder.getInstance());
        instanceList.fillList();
        
        controlOne.add(dbInstanceLabel);
        controlOne.add(instanceList.getCombo());
        controlOne.add(connectButton);
        
        connectButton.addActionListener(new ActionListener()               
            {                                                         
                public void actionPerformed(ActionEvent e)              
                    {
                        instanceList.initConnector();
                    }
            });
        userTextField = new JTextField();
        
        String fName = getUserFileName();
        respList = new RespList(userTextField,fName);        
        
        this.add(new JScrollPane(respList.getTable()),BorderLayout.CENTER);
        
        controlOne.add(userTextField);
        
        selectAllButton = new JButton("Select All");
        controlTwo.add(selectAllButton);
        
        deselectAllButton = new JButton("Deselect All");
        controlTwo.add(deselectAllButton);
        
        deleteSelectedButton = new JButton("Delete Selected");
        controlTwo.add(deleteSelectedButton);
        
        selectAllButton.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                            respList.selectAll();
                        }
                    }
                );

        deselectAllButton.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                            respList.deselectAll();
                        }
                    }
                );
        
        deleteSelectedButton.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                            respList.deleteSelected();
                        }
                    }
                );

        
        bottomPanel = new JPanel(new BorderLayout());
        this.add(bottomPanel,BorderLayout.SOUTH);
        
        bottomOne = new JPanel(new FlowLayout());
        bottomTwo = new JPanel(new FlowLayout());
        
        bottomPanel.add(bottomOne,BorderLayout.NORTH);
        bottomPanel.add(bottomTwo,BorderLayout.SOUTH);
        
        
        
        
        respList.render();
        fromDBButton = new JButton("Load From Apps");
        bottomOne.add(fromDBButton);
        fromDBButton.addActionListener (new ActionListener(){
         public void actionPerformed (ActionEvent e){
             respList.loadFromDB();
             respList.render();
         }   
        }
                );
        toDBButton = new JButton("Save To Apps");
        bottomOne.add(toDBButton);
        toDBButton.addActionListener (new ActionListener(){
         public void actionPerformed (ActionEvent e){
             respList.saveToDB();
             respList.render();
         }   
        }
                );
        
        
        fileName = new JTextField(fName);
        
        
        bottomTwo.add(fileName);
        
        
        loadButton = new JButton("Load File");
        bottomTwo.add(loadButton);
        
        saveButton = new JButton("Save File");
        bottomTwo.add(saveButton);
        
        saveAsButton = new JButton("Save As...");
        bottomTwo.add(saveAsButton);
        
        String path = System.getProperty("user.dir");
        fileChooser = new JFileChooser(path);

        loadButton.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                        
                        int res = fileChooser.showDialog(MainForm.this, "Load");
                        if(res == JFileChooser.APPROVE_OPTION){
                            String fName = fileChooser.getSelectedFile().getPath();
                            fileName.setText(fName);
                            setUserFileName(fName);
                            respList.setFileName(fName);
                            respList.loadFromXML();
                            respList.render();
                        }
                        
                    }
                }
                );
        
          saveButton.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                            respList.setFileName(fileName.getText());
                            respList.saveToXML();
                        
                    }
                }
                );
        
        saveAsButton.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                        
                        int res = fileChooser.showDialog(MainForm.this, "Save");
                        if(res == JFileChooser.APPROVE_OPTION){
                            String fName = fileChooser.getSelectedFile().getPath();
                            fileName.setText(fName);
                            setUserFileName(fName);
                            respList.setFileName(fName);
                            
                            respList.saveToXML();
                            
                        }
                        
                    }
                }
                );
        
        
        
        
        this.setVisible(true);
    }
}
